'''
Author: WuFeng <763467339@qq.com>
Date: 2024-11-19 15:48:03
LastEditTime: 2024-11-19 17:17:03
LastEditors: WuFeng <763467339@qq.com>
Description: PDF压缩
FilePath: \markdown-magic\python\service\pdfCompress.py
Copyright 版权声明
'''
import PyPDF2
from pdf_compression import compress_pdf

def compress_pdf(input_path, output_path):
    # 打开输入 PDF 文件
    with open(input_path, 'rb') as input_file:
        reader = PyPDF2.PdfReader(input_file)
        
        # 创建一个 PdfWriter 对象
        writer = PyPDF2.PdfWriter()
        
        # 将每一页添加到 PdfWriter 对象中
        for page in reader.pages:
            writer.add_page(page)
        
        # 删除不必要的元数据
        writer.add_metadata({
            '/Title': '',
            '/Author': '',
            '/Subject': '',
            '/Keywords': '',
            '/Producer': '',
            '/Creator': '',
            '/CreationDate': '',
            '/ModDate': '',
        })
        
        # 写入输出 PDF 文件
        with open(output_path, 'wb') as output_file:
            writer.write(output_file)



def compress_pdf_file(input_path, output_path):
    # 压缩 PDF 文件
    compress_pdf(input_path, output_path)